package com.itheima.fastjson.config;

import com.alibaba.fastjson.serializer.*;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 配置fastjson转换器 默认是jackson
 */
@Configuration
public class FastjsonConverConfig {


    @Bean
    public HttpMessageConverters httpMessageConverters(){
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        // 设置支持的MediaType
        List<MediaType> typeList = new ArrayList<>();
        typeList.add(MediaType.APPLICATION_JSON_UTF8); // APPLICATION_JSON_UTF8的将来被获取的值是application/json;charset=UTF-8
        fastJsonConverter.setSupportedMediaTypes(typeList);

        // 设置fastjson的全局配置(谨慎使用)
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        // 配置SerializerFeature - 为了数据序列化的格式化处理
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );
        // 配置SerializeConfig 能够制定具体类型使用的序列化转换器
        SerializeConfig serializeConfig = new SerializeConfig();
        serializeConfig.put(Date.class,new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); //类似设定全局日期转换
        // 模拟上面的序列化配置 自定义BigDecimal序列化器
        // 问题：当IdCard类中没有@JSONField注解并且没有format属性时，下面的这个配置不执行
//        serializeConfig.put(BigDecimal.class,new MyBigDecimalSerializer(2)); //类似设定全局日期转换
        fastJsonConfig.setSerializeConfig(serializeConfig);

        // 属性过滤器 after过滤器
        /*PropertyFilter propertyFilter = new PropertyFilter() {
            @Override
            public boolean apply(Object object, String name, Object value) {
                // BigDecimal类型的 不进行序列化；后面使用BeforeFilter/AfterFilter手动填充
                if(value instanceof BigDecimal)
                    return false;
                return true;
            }
        };
        AfterFilter afterFilter = new AfterFilter() {
            @Override
            public void writeAfter(Object object) {
                Field[] fields = object.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if (field.getType() == BigDecimal.class) {
                        field.setAccessible(true);
                        Object value= null;
                        try {
                            value = (BigDecimal)field.get(object);
                            value = ((BigDecimal) value).setScale(2,BigDecimal.ROUND_DOWN);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        // 将key value写入到序列化的字符串中
                        writeKeyValue(field.getName(), value );
                    }
                }
            }
        };*/
//        fastJsonConfig.setSerializeFilters(propertyFilter,afterFilter);
        ValueFilter valueFilter = new ValueFilter() {
            @Override
            public Object process(Object object, String name, Object value) {
                if(value instanceof BigDecimal){
                    value = ((BigDecimal)value).setScale(3,BigDecimal.ROUND_DOWN);
                }
                return value;
            }
        };
        fastJsonConfig.setSerializeFilters(valueFilter);
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastJsonConverter);
    }

}
